home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 335_01 / as2650.y < prev    next >
Text File  |  1990-12-02  |  41KB  |  1,634 lines

  1. %{
  2.  
  3. /*
  4. HEADER:     ;
  5. TITLE:         Frankenstein Cross Assemblers;
  6. VERSION:     2.0;
  7. DESCRIPTION: "    Reconfigurable Cross-assembler producing Intel (TM)
  8.         Hex format object records.  ";
  9. KEYWORDS:     cross-assemblers, 1805, 2650, 6301, 6502, 6805, 6809, 
  10.         6811, tms7000, 8048, 8051, 8096, z8, z80;
  11. SYSTEM:     UNIX, MS-Dos ;
  12. FILENAME:     as2650.y;
  13. WARNINGS:     "This software is in the public domain.  
  14.         Any prior copyright claims are relinquished.  
  15.  
  16.         This software is distributed with no warranty whatever.  
  17.         The author takes no responsibility for the consequences 
  18.         of its use.
  19.  
  20.         Yacc (or Bison) required to compile."  ;
  21. SEE-ALSO:     as2650.doc,frasmain.c;    
  22. AUTHORS:     Mark Zenier;
  23. COMPILERS:     Microport Sys V/AT, ATT Yacc, Turbo C V1.5, Bison (CUG disk 285)
  24.         (previous versions Xenix, Unisoft 68000 Version 7, Sun 3);
  25. */
  26. /* 2650 instruction generation file, standard syntax */
  27. /* November 17, 1990 */
  28.  
  29. /*
  30.     description    frame work parser description for framework cross
  31.             assemblers
  32.     history        February 2, 1988
  33.             September 11, 1990 - merge table definition
  34.             September 12, 1990 - short file names
  35.             September 14, 1990 - short variable names
  36.             September 17, 1990 - use yylex as external
  37. */
  38. #include <stdio.h>
  39. #include "frasmdat.h"
  40. #include "fragcon.h"
  41.  
  42. #define yylex lexintercept
  43.  
  44.     /* selectors for register */
  45.     /* 0000 0000 0000 xxxx */
  46. #define REGMASK    0xf 
  47. #define REG0    0x1
  48. #define REG1    0x2
  49. #define REG2    0x4
  50. #define REG3    0x8
  51.     /* selectors for conditions */
  52.     /* 0000 0000 xxxx 0000 */
  53. #define CONDMASK    0xf0
  54. #define COND0    0x10
  55. #define COND1    0x20
  56. #define COND2    0x40
  57. #define COND3 0x80
  58.  
  59. #define PAGEBITS 0x6000
  60. #define ST_INH 0x1
  61. #define ST_EXP 0x2
  62. #define ST_INDIR 0x4
  63. #define ST_REG 0x8
  64. #define ST_REGCOMMA 0x10
  65. #define ST_REGEXP 0x20
  66. #define ST_REGINDIR 0x40
  67. #define ST_COND 0x80
  68. #define ST_CONDEXP 0x100
  69. #define ST_CONDINDIR 0x200
  70. #define ST_BINDEX 0x400
  71. #define ST_BINDIRX 0x800
  72. #define    API_ABS    0
  73. #define    API_INC    2
  74. #define    API_DEC    4
  75. #define    API_IND    6
  76. #define    API_IABS    8
  77. #define    API_IINC    0xa
  78. #define    API_IDEC    0xc
  79. #define    API_IIND    0xe
  80. #define ST_ABSOLUTE 0x1
  81. #define ST_INDEX 0x2
  82. #define ST_INCINDEX 0x4
  83. #define ST_DECINDEX 0x8
  84. #define ST_AREGINDIR 0x10
  85. #define ST_INDIRX 0x20
  86. #define ST_INCINDIRX 0x40
  87. #define ST_DECINDIRX 0x80
  88.     
  89.     static int    regsel[4] = {REG0, REG1, REG2, REG3};
  90.     static int    condsel[4] = {COND0, COND1, COND2, COND3};
  91.     static int    prevpage;
  92.     static char    genbdef[] = "[1=];";
  93.     static char    genwdef[] = "[1=]x"; /* x for normal, y for byte rev */
  94.     char ignosyn[] = "[Xinvalid syntax for instruction";
  95.     char ignosel[] = "[Xinvalid operands";
  96.  
  97.     long    labelloc;
  98.     static int satsub;
  99.     int    ifstkpt = 0;
  100.     int    fraifskip = FALSE;
  101.  
  102.     struct symel * endsymbol = SYMNULL;
  103.  
  104. %}
  105. %union {
  106.     int    intv;
  107.     long     longv;
  108.     char    *strng;
  109.     struct symel *symb;
  110. }
  111.  
  112. %token <intv> REGISTER 
  113. %token <intv> CONDITION 
  114. %token <intv> KOC_BDEF
  115. %token <intv> KOC_ELSE
  116. %token <intv> KOC_END
  117. %token <intv> KOC_ENDI
  118. %token <intv> KOC_EQU
  119. %token <intv> KOC_IF
  120. %token <intv> KOC_INCLUDE
  121. %token <intv> KOC_ORG
  122. %token <intv> KOC_RESM
  123. %token <intv> KOC_SDEF
  124. %token <intv> KOC_SET
  125. %token <intv> KOC_WDEF
  126. %token <intv> KOC_CHSET
  127. %token <intv> KOC_CHDEF
  128. %token <intv> KOC_CHUSE
  129. %token <intv> KOC_ACON
  130. %token <intv> KOC_opcode
  131. %token <intv> KOC_indexabs
  132.  
  133. %token <longv> CONSTANT
  134. %token EOL
  135. %token KEOP_AND
  136. %token KEOP_DEFINED
  137. %token KEOP_EQ
  138. %token KEOP_GE
  139. %token KEOP_GT
  140. %token KEOP_HIGH
  141. %token KEOP_LE
  142. %token KEOP_LOW
  143. %token KEOP_LT
  144. %token KEOP_MOD
  145. %token KEOP_MUN
  146. %token KEOP_NE
  147. %token KEOP_NOT
  148. %token KEOP_OR
  149. %token KEOP_SHL
  150. %token KEOP_SHR
  151. %token KEOP_XOR
  152. %token KEOP_locctr
  153. %token <symb> LABEL
  154. %token <strng> STRING
  155. %token <symb> SYMBOL
  156.  
  157. %token KTK_invalid
  158.  
  159. %right    KEOP_HIGH KEOP_LOW
  160. %left    KEOP_OR KEOP_XOR
  161. %left    KEOP_AND
  162. %right    KEOP_NOT
  163. %nonassoc    KEOP_GT KEOP_GE KEOP_LE KEOP_LT KEOP_NE KEOP_EQ
  164. %left    '+' '-'
  165. %left    '*' '/' KEOP_MOD KEOP_SHL KEOP_SHR
  166. %right    KEOP_MUN
  167.  
  168.  
  169. %type <intv> expr exprlist stringlist
  170.  
  171. %start file
  172.  
  173. %%
  174.  
  175. file    :    file allline
  176.     |    allline
  177.     ;
  178.  
  179. allline    :     line EOL
  180.             {
  181.                 clrexpr();
  182.             }
  183.     |    EOL
  184.     |    error EOL
  185.             {
  186.                 clrexpr();
  187.                 yyerrok;
  188.             }
  189.     ;
  190.  
  191. line    :    LABEL KOC_END 
  192.             {
  193.                 endsymbol = $1;
  194.                 nextreadact = Nra_end;
  195.             }
  196.     |          KOC_END 
  197.             {
  198.                 nextreadact = Nra_end;
  199.             }
  200.     |    KOC_INCLUDE STRING
  201.             {
  202.         if(nextfstk >= FILESTKDPTH)
  203.         {
  204.             fraerror("include file nesting limit exceeded");
  205.         }
  206.         else
  207.         {
  208.             infilestk[nextfstk].fnm = savestring($2,strlen($2));
  209.             if( (infilestk[nextfstk].fpt = fopen($2,"r"))
  210.                 ==(FILE *)NULL )
  211.             {
  212.                 fraerror("cannot open include file");
  213.             }
  214.             else
  215.             {
  216.                 nextreadact = Nra_new;
  217.             }
  218.         }
  219.             }
  220.     |    LABEL KOC_EQU expr 
  221.             {
  222.                 if($1 -> seg == SSG_UNDEF)
  223.                 {
  224.                     pevalexpr(0, $3);
  225.                     if(evalr[0].seg == SSG_ABS)
  226.                     {
  227.                         $1 -> seg = SSG_EQU;
  228.                         $1 -> value = evalr[0].value;
  229.                         prtequvalue("C: 0x%lx\n",
  230.                             evalr[0].value);
  231.                     }
  232.                     else
  233.                     {
  234.                         fraerror(
  235.                     "noncomputable expression for EQU");
  236.                     }
  237.                 }
  238.                 else
  239.                 {
  240.                     fraerror(
  241.                 "cannot change symbol value with EQU");
  242.                 }
  243.             }
  244.     |    LABEL KOC_SET expr 
  245.             {
  246.                 if($1 -> seg == SSG_UNDEF
  247.                    || $1 -> seg == SSG_SET)
  248.                 {
  249.                     pevalexpr(0, $3);
  250.                     if(evalr[0].seg == SSG_ABS)
  251.                     {
  252.                         $1 -> seg = SSG_SET;
  253.                         $1 -> value = evalr[0].value;
  254.                         prtequvalue("C: 0x%lx\n",
  255.                             evalr[0].value);
  256.                     }
  257.                     else
  258.                     {
  259.                         fraerror(
  260.                     "noncomputable expression for SET");
  261.                     }
  262.                 }
  263.                 else
  264.                 {
  265.                     fraerror(
  266.                 "cannot change symbol value with SET");
  267.                 }
  268.             }
  269.     |    KOC_IF expr 
  270.             {
  271.         if((++ifstkpt) < IFSTKDEPTH)
  272.         {
  273.             pevalexpr(0, $2);
  274.             if(evalr[0].seg == SSG_ABS)
  275.             {
  276.                 if(evalr[0].value != 0)
  277.                 {
  278.                     elseifstk[ifstkpt] = If_Skip;
  279.                     endifstk[ifstkpt] = If_Active;
  280.                 }
  281.                 else
  282.                 {
  283.                     fraifskip = TRUE;
  284.                     elseifstk[ifstkpt] = If_Active;
  285.                     endifstk[ifstkpt] = If_Active;
  286.                 }
  287.             }
  288.             else
  289.             {
  290.                 fraifskip = TRUE;
  291.                 elseifstk[ifstkpt] = If_Active;
  292.                 endifstk[ifstkpt] = If_Active;
  293.             }
  294.         }
  295.         else
  296.         {
  297.             fraerror("IF stack overflow");
  298.         }
  299.             }
  300.                         
  301.     |    KOC_IF 
  302.             {
  303.         if(fraifskip) 
  304.         {
  305.             if((++ifstkpt) < IFSTKDEPTH)
  306.             {
  307.                     elseifstk[ifstkpt] = If_Skip;
  308.                     endifstk[ifstkpt] = If_Skip;
  309.             }
  310.             else
  311.             {
  312.                 fraerror("IF stack overflow");
  313.             }
  314.         }
  315.         else
  316.         {
  317.             yyerror("syntax error");
  318.             YYERROR;
  319.         }
  320.                 }
  321.                         
  322.     |    KOC_ELSE 
  323.             {
  324.                 switch(elseifstk[ifstkpt])
  325.                 {
  326.                 case If_Active:
  327.                     fraifskip = FALSE;
  328.                     break;
  329.                 
  330.                 case If_Skip:
  331.                     fraifskip = TRUE;
  332.                     break;
  333.                 
  334.                 case If_Err:
  335.                     fraerror("ELSE with no matching if");
  336.                     break;
  337.                 }
  338.             }
  339.  
  340.     |    KOC_ENDI 
  341.             {
  342.                 switch(endifstk[ifstkpt])
  343.                 {
  344.                 case If_Active:
  345.                     fraifskip = FALSE;
  346.                     ifstkpt--;
  347.                     break;
  348.                 
  349.                 case If_Skip:
  350.                     fraifskip = TRUE;
  351.                     ifstkpt--;
  352.                     break;
  353.                 
  354.                 case If_Err:
  355.                     fraerror("ENDI with no matching if");
  356.                     break;
  357.                 }
  358.             }
  359.     |    LABEL KOC_ORG expr 
  360.             {
  361.                 pevalexpr(0, $3);
  362.                 if(evalr[0].seg == SSG_ABS)
  363.                 {
  364.                     locctr = labelloc = evalr[0].value;
  365.                     if($1 -> seg == SSG_UNDEF)
  366.                     {
  367.                         $1 -> seg = SSG_ABS;
  368.                         $1 -> value = labelloc;
  369.                     }
  370.                     else
  371.                         fraerror(
  372.                         "multiple definition of label");
  373.                     prtequvalue("C: 0x%lx\n",
  374.                         evalr[0].value);
  375.                 }
  376.                 else
  377.                 {
  378.                     fraerror(
  379.                      "noncomputable expression for ORG");
  380.                 }
  381.             }
  382.     |          KOC_ORG expr 
  383.             {
  384.                 pevalexpr(0, $2);
  385.                 if(evalr[0].seg == SSG_ABS)
  386.                 {
  387.                     locctr = labelloc = evalr[0].value;
  388.                     prtequvalue("C: 0x%lx\n",
  389.                         evalr[0].value);
  390.                 }
  391.                 else
  392.                 {
  393.                     fraerror(
  394.                      "noncomputable expression for ORG");
  395.                 }
  396.             }
  397.     |    LABEL KOC_CHSET
  398.             {
  399.                 if($1 -> seg == SSG_UNDEF)
  400.                 {
  401.                     $1 -> seg = SSG_EQU;
  402.                     if( ($1->value = chtcreate()) <= 0)
  403.                     {
  404.         fraerror( "cannot create character translation table");
  405.                     }
  406.                     prtequvalue("C: 0x%lx\n", $1 -> value);
  407.                 }
  408.                 else
  409.                 {
  410.             fraerror( "multiple definition of label");
  411.                 }
  412.             }
  413.